import { defineStore } from "pinia";
import { ref } from "vue";
import { getloginapi, mergecartapi } from "@/apis/layout.js";
import { useCartStore } from "./cartStore.js";

export const useUserStore = defineStore(
  "user",
  () => {
    const userStore = useCartStore();
    // 1. 定义管理用户数据的state
    const loginList = ref({});
    // 2. 定义获取接口数据的action函数
    const getlogin = async (params) => {
      const res = await getloginapi(params);
      loginList.value = res.data.result;
      await mergecartapi(
        userStore.cartList.map((item) => {
          return {
            skuId: item.skuId,
            selected: item.selected,
            count: item.count,
          };
        })
      );
      useCartStore.updatedcart();
    };
    // 3. 以对象的格式把state和action return
    return {
      loginList,
      getlogin,
    };
  },
  {
    persist: true,
  }
);
